package com.example.cs4.service.impl;

import com.example.cs4.mapper.UserServiceMapper;
import com.example.cs4.model.User;
import com.example.cs4.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.constraints.Negative;
import java.util.Collections;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserServiceMapper userServiceMapper;

    @Override
    public User getUserById(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        User user = userServiceMapper.getUserById(id);
        if (user == null) {
            throw new IllegalArgumentException("ID不存在");
        }
        return user;
    }

    @Override
    public void saveUser(User user) {
        if (user == null) {
            throw new IllegalArgumentException("用户不能为空");
        }
        if (user.getName() == null || user.getPassword() == null) {
            throw new IllegalArgumentException("用户名或密码不能为空");
        }
        User user1 = userServiceMapper.getUserByName(user.getName());
        if (user1 != null) {
            throw new IllegalArgumentException("用户名已存在");
        }
        try {
            userServiceMapper.saveUser(user);
            System.out.println("用户注册成功");
        } catch (Exception e) {
            throw new IllegalArgumentException("注册用户失败: " + e.getMessage());
        }
    }

    @Override
    public User login(String name, String password) {
        if (name == null || password == null) {
            throw new IllegalArgumentException("用户名或密码不能为空");
        }
        try {
            User user = userServiceMapper.login(name, password);
            if (user == null) {
                throw new IllegalArgumentException("用户名或密码错误");
            }
            return user;
        } catch (Exception e) {
            throw new IllegalArgumentException("登录失败: " + e.getMessage());
        }
    }

    @Override
    public User getUserByName(String name) {
        if (name == null) {
            throw new IllegalArgumentException("用户名不能为空");
        }
        User user = userServiceMapper.getUserByName(name);
        if (user == null) {
            throw new IllegalArgumentException("用户名不存在");
        }
        return user;
    }

    @Override
    public User deleteUser(Integer id) {
        if (id == null) {
            throw new IllegalArgumentException("ID不能为空");
        }
        User user = userServiceMapper.getUserById(id);
        if (user == null) {
            throw new IllegalArgumentException("ID不存在");
        }
        try {
            if (user == null) {
                throw new IllegalArgumentException("删除用户失败");
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("删除用户失败");
        }
        userServiceMapper.deleteUserById(id);
        return user;
    }

    @Override
    public User updateUser(String name, String password) {
        if (name == null || password == null) {
            throw new IllegalArgumentException("用户名或密码不能为空");
        }
        try {
            User user = userServiceMapper.updateUser(name, password);
            if (user == null) {
                throw new IllegalArgumentException("更新用户失败");
            }
        } catch (Exception e) {
            throw new IllegalArgumentException("更新用户失败");
        }

        return null;
    }

    @Override
    public List<User> getPage(Integer PageNum, Integer PageSize) {
        // 参数校验
        if (PageNum == null || PageSize == null) {
            throw new IllegalArgumentException("页码和大小不能为空");
        }

        // 计算偏移量
        int offset = (PageNum - 1) * PageSize;

        // 调用 Mapper 方法查询数据
        List<User> users = userServiceMapper.getPage(offset, PageSize);

        // 判断查询结果是否为空
        if (users == null || users.isEmpty()) {
            throw new IllegalArgumentException("获取用户失败或无数据");
        }

        return users;
    }

    @Override
    public List<User> getAll(String name) {
        if (name == null){
            throw new IllegalArgumentException("用户名不能为空");
        }try{
            List<User> users = userServiceMapper.getAll(name);
            if (users == null || users.isEmpty()){
                return Collections.emptyList();
            }
            return users;
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("获取用户失败: " + e.getMessage());
        }
    }
}
